package com.cms.server.resolver.plugins.scsq;

import java.sql.ResultSet;
import java.util.Date;

import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;

/**
 * <p>Title:流程节点提交、取回、退回 </p>
 * <p>Description:
 * 1.流程节点提交，如果下个节点是会签节点，则跳过不处理，在下个插件里处理
 * 2.流程节点取回，如果本节点是会签节点，则跳过不处理，在下个插件里处理
 * 3.流程节点退回，如果退回到会签节点，则跳过不处理，在下个插件里处理
 *  </p>
 * <p>Copyright: Copyright (c) 2014</p>
 * @author prayer
 * @version 1.0
 *
 */
public class CMSSubmitToFlow extends FormDataResolverAdapter {

	//初始化FormDataModel
	public void finishSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {
		EFDataSet                    billDataSet = model.getBillDataSet();
		EFDataSet                     flowNodeDS = null;
		EFRowSet                      flowRowset = EFRowSet.getInstance();
		EFRowSet                      nodeRowset = EFRowSet.getInstance();
		EFRowSet                  nextNodeRowset = EFRowSet.getInstance();
		String                            strSql = "";
		ResultSet                             rs = null;
		long                             curTime = new Date().getTime();
		String                            F_KJQJ = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
		String                            F_GUID = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_GUID_COL_, SYS_MDL_VAL.BILL_GUID), "");
		String                            F_DJBH = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
		String                           FLOW_ID = PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "");
		String                           NODE_ID = PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "");
		String                     RESR_IN_CAUSE = PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "");

		//获取提交到下一个节点名称
		if(RESR_IN_CAUSE.equals("submit")) {
			//提交的时候先获取节点信息，判断是不是会签节点，如果是，则取流程节点信息的时候加上TASK_TO_UNIT条件过滤
			//获取节点信息
			strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = '" + NODE_ID + "'";
			rs = stmt.executeQuery(strSql);
			if(rs != null && rs.next()) nodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowset);
			
			//获取流程节点
			strSql = "select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
			       + " FLOW_ID = '" + FLOW_ID + "' and NODE_TAG = '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "' ";
			//如果是会签节点提交，需要加上TASK_TO_UNIT的条件
			if(nodeRowset.getString("NODE_TYPE", "").equals("countersign")) {
				strSql += " and TASK_TO_UNIT = '" + PO.GetValueByParamName("OP_BMBH", "") + "'";
			}
			strSql += "  order by OP_ID DESC";
			rs = stmt.executeQuery(strSql);
			flowNodeDS = DataSetUtils.resultSet2DataSet(rs, flowNodeDS);
			flowRowset = flowNodeDS.getRowSet(0);
			//获取提交到节点名称
			strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = '" + flowRowset.getString("NODE_TAG", "") + "'";
			rs = stmt.executeQuery(strSql);
			if(rs != null && rs.next()) nodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowset);
			
			//如果是会签节点，则RESR_STATUS不能为pending而应该为waiting，等所有会签节点都提交确认后才能提交到下一个节点
			if(nodeRowset.getString("NODE_TYPE", "").equals("countersign")) {
				//从会签节点提交到下一个节点，并且下一个节点为自动提交节点，所以把之前的状态存储起来
				PO.SetValueByParamName(FLOW_TASK_LIST._RESR_STATUS + "_BACK", PO.GetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, ""));
				PO.SetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "waiting");
			}
			
			//会签节点下一个节点退回部门后，部门再次提交F_CHAR19才会有值
			//比如：会签节点下一个节点退回0001部门，那么0002和0003部门是还原到之前的等待状态，下一步的更新会根据F_CHAR19来，所以更新上
			if(flowRowset.getString("F_CHAR19", "").equals("")) {
				PO.SetValueByParamName("F_CHAR19", flowRowset.getString("TASK_TO_UNIT", ""));
				PO.SetValueByParamName("F_CHAR20", flowRowset.getString("TASK_TO_UNIT_NAME", ""));
			}
			//如果是退回的部门，F_CHAR19有值
			else {
				PO.SetValueByParamName("F_CHAR19", flowRowset.getString("F_CHAR19", ""));
				PO.SetValueByParamName("F_CHAR20", flowRowset.getString("F_CHAR20", ""));
			}
			
			strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = '" + nodeRowset.getString("NODE_NEXT", "") + "'";
			rs = stmt.executeQuery(strSql);
			if(rs != null && rs.next()) {
				nextNodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nextNodeRowset);
				//如果下个节点是会签节点，则直接返回
				if(nextNodeRowset.getString("NODE_TYPE", "").equals("countersign")) {
					return;
				}
			}
		} 
		//提交到本节点
		else if(RESR_IN_CAUSE.equals("retake")) {
			//获取流程节点
			strSql = "select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
			       + " FLOW_ID = '" + FLOW_ID + "' and NODE_SRC = '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "' order by OP_ID DESC";
			rs = stmt.executeQuery(strSql);
			flowNodeDS = DataSetUtils.resultSet2DataSet(rs, flowNodeDS);
			flowRowset = flowNodeDS.getRowSet(0);
//			PO.SetValueByParamName("F_CHAR19", flowRowset.getString("F_CHAR19", ""));
//			PO.SetValueByParamName("F_CHAR20", flowRowset.getString("F_CHAR20", ""));
			//获取提交到节点名称
			strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = '" + flowRowset.getString("NODE_TAG", "") + "'";
			rs = stmt.executeQuery(strSql);
			if(rs != null && rs.next()) nodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowset);
			//如果当前节点是会签节点，则返回
			if(nodeRowset.getString("NODE_TYPE", "").equals("countersign")) return;
			strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_NEXT = '" + nodeRowset.getString("NODE_ID", "") + "'";
			rs = stmt.executeQuery(strSql);
			if(rs != null && rs.next()) {
				nextNodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nextNodeRowset);
				//如果取回到会签节点，则需要更新TASK_TO_UNIT,TASK_TO_UNIT_NAME
				if(nextNodeRowset.getString("NODE_TYPE", "").equals("countersign")) {
					PO.SetValueByParamName("TASK_TO_UNIT", flowRowset.getString("F_CHAR19", ""));
					PO.SetValueByParamName("TASK_TO_UNIT_NAME", flowRowset.getString("F_CHAR20", ""));
				}
			}
		}
		//退回到上一个节点
		else if(RESR_IN_CAUSE.equals("rollback")) {
			//先获取节点的上一个节点，如果是会签节点的下一个节点，则是分部门退回的，不在这里处理
			strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_NEXT = '" + PO.GetValueByParamName("OP_NODE", "") + "'";
			rs = stmt.executeQuery(strSql);
			if(rs != null && rs.next()) nodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowset);
			if(nodeRowset.getString("NODE_TYPE", "").equals("countersign")) return;
			
			//先获取节点信息，再获取流程节点信息，判断是否为会签节点，决定是否加上部门条件
			strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = '" + PO.GetValueByParamName("OP_NODE", "") + "'";
			rs = stmt.executeQuery(strSql);
			if(rs != null && rs.next()) nodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowset);
			
			//获取流程节点
			strSql = "select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
			       + " FLOW_ID = '" + FLOW_ID + "' and NODE_TAG = '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "'";
			if(nodeRowset.getString("NODE_TYPE", "").equals("countersign")) {
				strSql += " and TASK_TO_UNIT = '" + PO.GetValueByParamName("OP_BMBH", "") + "'";
			}
			strSql += " order by OP_ID DESC";
			rs = stmt.executeQuery(strSql);
			flowNodeDS = DataSetUtils.resultSet2DataSet(rs, flowNodeDS);
			flowRowset = flowNodeDS.getRowSet(0);
			PO.SetValueByParamName("F_CHAR19", flowRowset.getString("F_CHAR19", ""));
			PO.SetValueByParamName("F_CHAR20", flowRowset.getString("F_CHAR20", ""));
			//获取提交到节点名称
			strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = '" + flowRowset.getString("NODE_TAG", "") + "'";
			rs = stmt.executeQuery(strSql);
			if(rs != null && rs.next()) nodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowset);
			//如果会签节点退回，则直接退回到开始节点
			if(nodeRowset.getString("NODE_TYPE", "").equals("countersign")) {
				PO.SetValueByParamName("F_CHAR19", flowRowset.getString("TASK_TO_UNIT", ""));
				PO.SetValueByParamName("F_CHAR20", flowRowset.getString("TASK_TO_UNIT_NAME", ""));
				strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_TYPE = 'start'";
				rs = stmt.executeQuery(strSql);
				if(rs != null && rs.next()) nextNodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nextNodeRowset);
			}
			//否则退回到上级节点
			else {
				strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_NEXT = '" + nodeRowset.getString("NODE_ID", "") + "'";
				rs = stmt.executeQuery(strSql);
				if(rs != null && rs.next()) nextNodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nextNodeRowset);
			}
		}
		
		//更新上一个任务节点的状态
		strSql = "update FLOW_TASK_LIST set RESR_STATUS = 'processed', RESR_OUT_CAUSE = '" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_OUT_CAUSE, "") + "'"
			   + " where OP_ID = '" + flowRowset.getString("OP_ID", "") + "' and FLOW_ID = '" + FLOW_ID + "'";
		stmt.execute(strSql);
		
		strSql = "insert into FLOW_TASK_LIST (OP_ID,FLOW_ID,BIZ_DJBH,BIZ_DATE,BIZ_GUID,OP_USER,OP_USER_NAME,OP_TIME,"
			   + "OP_BMBH,OP_NOTE,TASK_STATUS,OP_NODE,NODE_SRC,NODE_SRC_NAME,NODE_TAG,NODE_TAG_NAME,POP_ID,RESR_STATUS,TASK_TO_UNIT,TASK_TO_UNIT_NAME," 
			   + " TASK_TO_USER, TASK_TO_USER_NAME, ";
	 	
		for(int j = 1; j <= 20; j++) {
			if(j < 10) {
				if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
					strSql += "F_CHAR0" + j + ",";
				}
				if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
					strSql += "F_DATE0" + j + ",";
				}
				if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
					strSql += "F_NUM0" + j + ",";
				}
			} else {
				if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) != null) {
					strSql += "F_CHAR" + j + ",";
				}
				if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) != null) {
					strSql += "F_DATE" + j + ",";
				}
				if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) != null) {
					strSql += "F_NUM" + j + ",";
				}
			}
		}
	 	strSql += "RESR_IN_CAUSE,RESR_OUT_CAUSE) values ('"
	 	        + curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "") + "', '"
				+ F_DJBH + "', '" + F_KJQJ + "', '" + F_GUID + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER, "")
				+ "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, "") + "', '"
				+ curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_BMBH, "") + "', '" 
				+ PO.GetValueByParamName(FLOW_TASK_LIST._OP_NOTE, "") + "', 'processing', '"
				+ PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '" + nodeRowset.getString("NODE_ID", "") + "', '"
				+ nodeRowset.getString("NODE_NAME", "") + "', '" + nextNodeRowset.getString("NODE_ID", "") + "', '" + nextNodeRowset.getString("NODE_NAME", "") 
				+ "', '" + flowRowset.getString("OP_ID", "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "") 
				+ "','','','" + PO.GetValueByParamName(FLOW_TASK_LIST._TASK_TO_USER, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._TASK_TO_USER_NAME, "") + "', ";
	 	for(int j = 1; j <= 20; j++) {
	 		if(j < 10) {
	 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
	 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null)  + "',";
	 			} 
	 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
	 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null)  + "',";
	 			}
	 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
	 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null)  + "',";
	 			}
	 		} else {
	 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) != null) {
	 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null)  + "',";
	 			} 
	 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) != null) {
	 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null)  + "',";
	 			}
	 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) != null) {
	 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null)  + "',";
	 			}
	 		}
	 	}
	 	strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "") + "', '')";
	 	PO.SetValueByParamName(FLOW_TASK_LIST._OP_ID, String.valueOf(curTime));
//	 	PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE, nodeRowset.getString("NODE_ID", ""));
	 	PO.SetValueByParamName("OP_NODE_NEXT", nextNodeRowset.getString("NODE_ID", ""));
		stmt.execute(strSql);
	}
}
